package cn.ffcs.uec.organization.action;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Tab;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.portal.theme.ThemeDisplay;
import cn.ffcs.uec.organization.action.bean.UecOrganizationTreeMainBean;
import cn.ffcs.uec.organization.constant.UecOrganizationConstant;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.organization.vo.OrganizationRelationVo;
import cn.ffcs.uec.post.model.UecPost;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.UomZkUtil;

@Controller
@Scope("prototype")
public class UecOrganizationTreeMainComposer extends BasePortletComposer implements IPortletInfoProvider {

	private static final long serialVersionUID = -3342502678426130037L;

	private Logger logger = Logger.getLogger(this.getClass());

	private UecOrganizationTreeMainBean bean = new UecOrganizationTreeMainBean();

//	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
//			.getBean("uecOrgOrgsManager");
//	private UecOrgDeptManager uecOrgDeptManager = (UecOrgDeptManager) ApplicationContextUtil
//			.getBean("uecOrgDeptManager");
//	/**
//	 * 选中的组织/部门 组织与部门分别在两张表中
//	 */
//	private UecOrgOrgs uecOrgOrgs;
//	private UecOrgDept uecOrgDept;

	OrganizationRelationVo organizationRelationVo;

	@Override
	public String getPortletId() {
		return super.getPortletId();
	}

	@Override
	public ThemeDisplay getThemeDisplay() {
		return super.getThemeDisplay();
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		UomZkUtil.autoFitHeight(comp);// 使用<borderlayout>时，需要加上这句进行初始化，不然从仓库调出的页面，不能显示
		Components.wireVariables(comp, bean);
		this.bean.getUecPoliticalTreeExt().setPortletInfoProvider(this);

		// this.bean.getUecOrganizationInfoExt().setPortletInfoProvider(this);
		// this.bean.getUecDeptInfoExt().setPortletInfoProvider(this);

		/**
		 * 选中内部树上的（组织关系）
		 */
		this.bean.getUecPoliticalTreeExt().addForward(UecOrganizationConstant.ON_SELECT_ORGREATION_TREE_REQUEST,
				this.self, "onSelectOrgReationTreeResponse");
	}

	/**
	 * 界面初始化
	 */
	public void onCreate$uecOrganizationTreeMainWindow() {

	}

	/**
	 * 选中的内部树组织关系
	 * 
	 * @param event
	 */
	public void onSelectOrgReationTreeResponse(ForwardEvent event) {
		organizationRelationVo = (OrganizationRelationVo) event.getOrigin().getData();
		if (null != organizationRelationVo) {
			logger.info("进入组织树主组件");
			postOrgOrgsEvent();
			postOrgDeptEvent();
			postUecPostEvent();
		}
	}

	/**
	 * 根据组织关系获取各对象（组织还是部门）
	 */
	public void organizationVoTransfOrgOrDept(OrganizationRelationVo organizationRelationVo) {
		// if ("ORG".equals(organizationRelationVo.getOrgType())) {
		// this.bean.getUecOrganizationInfoExt().setHeight("100%");
		// this.bean.getUecDeptInfoExt().setHeight("0%");
		// this.bean.getUecOrganizationInfoExt().setVisible(true);
		// this.bean.getUecDeptInfoExt().setVisible(false);
		// uecOrgOrgs = uecOrgOrgsManager
		// .findUecOrgOrgsByOrganizationRelationVo(organizationRelationVo);
		// Events.postEvent(
		// UecOrganizationConstant.ON_SELECT_UECORGORGS_TREE_REQUEST,
		// this.bean.getUecOrganizationInfoExt(), uecOrgOrgs);
		// } else {
		// Events.postEvent(
		// UecOrganizationConstant.ON_CLEAN_SELECT_UECORGORGS_TREE_REQUEST,
		// this.bean.getUecOrganizationInfoExt(), null);
		// }
		// if ("DEPT".equals(organizationRelationVo.getOrgType())) {
		// this.bean.getUecDeptInfoExt().setHeight("100%");
		// this.bean.getUecOrganizationInfoExt().setHeight("0%");
		// this.bean.getUecDeptInfoExt().setVisible(true);
		// this.bean.getUecOrganizationInfoExt().setVisible(false);
		// uecOrgDept = uecOrgDeptManager
		// .findUecOrgDeptByOrganizationRelationVo(organizationRelationVo);
		// Events.postEvent(
		// UecOrganizationConstant.ON_SELECT_UECORGDEPT_TREE_REQUEST,
		// this.bean.getUecDeptInfoExt(), uecOrgDept);
		// } else {
		// Events.postEvent(
		// UecOrganizationConstant.ON_CLEAN_SELECT_UECORGDEPT_TREE_REQUEST,
		// this.bean.getUecDeptInfoExt(), null);
		// }
	}

	/**
	 * 点击tab
	 * 
	 * @throws Exception
	 */
	public void onClickTab(ForwardEvent forwardEvent) throws Exception {
		Event event = forwardEvent.getOrigin();
		if (event != null) {
			Component component = event.getTarget();
			if (component != null && component instanceof Tab) {
				final Tab clickTab = (Tab) component;
				bean.setSelectTab(clickTab);
				callTab();
			}
		}
	}

	/**
	 * 组织查询抛出事件
	 */
	public void postOrgOrgsEvent() {
		UecOrgOrgs uecOrgOrgs = UecOrgOrgs.newInstance();
		if ("ORG".equals(organizationRelationVo.getOrgType())) {
			uecOrgOrgs.setPkOrg(organizationRelationVo.getOrg());
			Events.postEvent("onOrganizationOrgsQuery", bean.getUecOrganizationOrgsListboxExt(), uecOrgOrgs);
		}
	}

	/**
	 * 部门查询抛出事件
	 */
	public void postOrgDeptEvent() {
		Events.postEvent("onOrganizationDeptQuery", bean.getUecOrganizationDeptsListboxExt(), organizationRelationVo);
	}

	/**
	 * 岗位查询抛出事件
	 */
	public void postUecPostEvent() {
		UecPost uecPost = UecPost.newInstance();
		if ("ORG".equals(organizationRelationVo.getOrgType())) {
			uecPost.setPkOrg(organizationRelationVo.getOrg());
		} else if ("DEPT".equals(organizationRelationVo.getOrgType())) {
			uecPost.setPkDept(organizationRelationVo.getOrg());
		}
		Events.postEvent("onOrgPostQuery", bean.getUecPostListboxExt(), uecPost);
	}

	/**
	 * @throws Exception
	 */
	public void callTab() throws Exception {
		if (this.bean.getSelectTab() == null) {
			bean.setSelectTab(this.bean.getTabBox().getSelectedTab());
		}
		if (organizationRelationVo != null) {
			if ("orgTab".equals(this.bean.getSelectTab().getId())) {
				postOrgOrgsEvent();
			} else if ("orgDept".equals(this.bean.getSelectTab().getId())) {
				postOrgDeptEvent();
			} else if ("orgPostTab".equals(bean.getSelectTab().getId())) {
				postUecPostEvent();
			}
		}
	}
}
